<?php
Class C_request extends Cemvc_Control_Base
{
  private $db;
  public function __construct()
  {
    parent::__construct();

    $this->view->assign("session", $_SESSION);
  }

  public function messages()
  {
    $req = new M_qin_request();
    /* $req->SetDbCodeNames("utf8"); */
    $res = $req->get_request($_SESSION["id"]);
    /* print_r($res); */
    $this->view->assign("messages", $res);
    
    /* $this->view->assign("messages", $_SESSION["messages"]); */
    $this->display();
  }

  /* parameter, id(id, primary key in request table) */
  /* insert the request in relation table, and set state to 'agree' */
  public function agree()
  {
    if (!isset($_REQUEST["id"]) && empty($_REQUEST["id"]))
      die(json_encode(array("resp_id"=>RESP_PARA_ERROR, "resp_str"=>"you must specify an id to handle;")));
    
    $request = new M_qin_request();
    $req = $request->get_req_info($_REQUEST["id"]);

    /* insert into relation table */
    $relation = new M_qin_relation();
    $request->mysqlQuery("start transaction");
    $relation->add($req["user_id"], $req["rel_id"], $req["rel_type"]);

    /* set state to agree in request table */
    $request->agree_request($_REQUEST["id"]);

    /* commit the transaction */
    $request->mysqlQuery("commit");
    $request->query();

    die(json_encode(array("resp_id"=>RESP_OK, "resp_str"=>"成功处理请求！")));
  }

  /* set state to 'cancel' */  
  public function cancel()
  {
    if (!isset($_REQUEST["id"]) && empty($_REQUEST["id"]))
      die(json_encode(array("resp_id"=>RESP_PARA_ERROR, "resp_str"=>"you must specify an id to handle;")));

    $request = new M_qin_request();
    $request->cancel_request($_REQUEST["id"]);

    die(json_encode(array("resp_id"=>RESP_OK, "resp_str"=>"成功处理请求！")));
  }
}